<?php 



if(array_key_exists('action', $_GET)){
    require_once("includes/config.php");
    if($_GET['action'] == 'search'){
        $search = $_POST['search_value'];
        //store query
        $store_query = "SELECT [Store_ID],[Store_Name],[Store_Info]
                        FROM [t_Stores] 
                        where [Store_Name] = '".$search."'";
        $store_result = $DBOBJ->select($store_query);
        echo $Store_ID = $store_result[0]['Store_ID'];
        exit;
    }
}


require_once("includes/header.php");
require_once("includes/Paginator.php");
//require_once("includes/left_panel.php");
$itemsPerPageReviews = 10;
$currentPageReviews = 1;
$fieldname = 'SR_ID';
$orderby = 'asc';
$link_page = '';




if(array_key_exists('pageno', $_GET)){
    if($_GET['pageno']){
        $currentPageReviews = $_GET['pageno'];
    }
}
if(array_key_exists('orderby', $_GET)){
    if($_GET['orderby']){
        $orderby = $_GET['orderby'];
        $link_page ="&orderby=".$orderby."";
    }
}
if(array_key_exists('field', $_GET)){
    if($_GET['field']){
        $fieldname = $_GET['field'];
        $link_page .= "&field=".$_GET['field']."";
    }
}
$offset = ($currentPageReviews - 1) * $itemsPerPageReviews;
$endlimit = $offset + $itemsPerPageReviews;
$request_count_query = "SELECT  [SR_ID]
                        ,[Store_ID]
                        ,[Prob_Desc]
                        ,[Status]
                        ,[Submitted_On]
                        ,[Location_id]
                        ,[service_request_number]
                    FROM [t_ServiceRequests]  ";
$request_count_result = $DBOBJ->select($request_count_query);

    
        
$request_query = "SELECT  * FROM (
                            SELECT *,ROW_NUMBER() OVER (ORDER BY ".$fieldname." ".$orderby.") AS RN
                    FROM [t_ServiceRequests] ) AS X
                    WHERE RN > ".$offset."
                    AND RN <= ".$endlimit."
                    ";
$request_result = $DBOBJ->select($request_query);




$paginator = new GTL_Paginator();
$paginator->setItemsPerPage($itemsPerPageReviews);
$paginator->setItemsTotal(count($request_count_result));
$paginator->setCurrentPage($currentPageReviews);



        
        
$link =$_SESSION['APP_FOLDER'].'/home.php?pageno=PAGENO'.$link_page;
$paginator->paginate($link);


//store query
$store_query = "SELECT [Store_ID],[Store_Name],[Store_Info]
                FROM [t_Stores]";
$store_result = $DBOBJ->select($store_query);
$storeData = '';
foreach ($store_result as $value){
    $storeData .='"'.$value['Store_Name'].'",';
}
$finalStoreData = rtrim($storeData, ",");

//location query
$location_query = "SELECT *
                    FROM [location]
                    INNER JOIN [t_Stores]
                    ON [location].[Store_Id]= [t_Stores].[Store_ID]";
$location_result = $DBOBJ->select($location_query);
$locationData = '';




foreach ($location_result as $loc_value){
   $locationData .="['<a href=\"location.php?store_id=".$loc_value['Store_Id']."\">".$loc_value['Store_Name']."</a><br>".$loc_value['Address1'].", ".$loc_value['City'].", ".$loc_value['Province']."',".$loc_value['Lattitude'].", ".$loc_value['Longitude']."],";
}
$finalLocationData = rtrim($locationData, ",");

?>
<link rel="stylesheet" href="css/jquery.ui.css">
<script src="js/jquery-ui.js"></script>
<script src="js/home.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false" 
          type="text/javascript"></script>


<script type="text/javascript">
    $(document).ready(function(){
    var locations = [
        <?php echo $finalLocationData;?>
    ];

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 10,
      center: new google.maps.LatLng(28.5267266, -81.4648804),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    for (i = 0; i < locations.length; i++) {  
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][1], locations[i][2]),
        map: map
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][0]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
    });
  </script>
  
  
  <script>
  $(function() {
    var availableTags = [
      <?php echo $finalStoreData;?>
    ];
    $( "#store" ).autocomplete({
      source: availableTags
    });
  });
  </script>

 

 
    
<!-- main container -->
    <div class="content">
         <div class="container-fluid">
            <div id="pad-wrapper">
                <div class="map-wrapper map section">
                    <div class="row-fluid head">
                        <div class="span12 map_img_container">
                        
                            <form action="" method="post">
                            <div class="map_search"> 
                            	<div class="ui-widget">
                                    <input type="text"  id="store" > 
                                    <input type="submit" value="" class="search_btn"    >
                               </div>
                            </div>
                            </form>
                            <!--<img src="img/map/map.png" alt="map_image" />-->
                             <div id="map" style="width: 600px; height: 963px;"></div>
                        </div>
                    </div>
                </div>    
                <!-- products table-->
                <!-- the script for the toggle all checkboxes from header is located in js/theme.js -->
                <div class="table-wrapper products-table section map-content">
                    <div class="row-fluid head">
                        <div class="span12">
                            <h4>Facility Dashboard</h4>
                        </div>
                    </div>
                    <div class="row-fluid filter-block">
                        <div class="pull-right top_moduel_container">
                            <div class=" open_tickets top_moduel ">
                              	  <div class="top_moduel_title"> OPEN TICKETS </div>
                                  <div class="top_moduel_number"> 8149 </div>
                                  <div class="top_moduel_graph"> <img src="img/map/map_graph_1.png" alt="map_image" /> </div>
                            </div>
                            
                             <div class="vendor_working top_moduel">
                              	  <div class="top_moduel_title"> VENDORS WORKING </div>
                                  <div class="top_moduel_number">150</div>
                                  <div class="top_moduel_graph"> <img src="img/map/map_graph_2.png" alt="map_image" /> </div>
                            </div>
                            
                             <div class="locations top_moduel">
                              	  <div class="top_moduel_title"> LOCATIONS </div>
                                  <div class="top_moduel_number"> 43,748 </div>
                                  <div class="top_moduel_graph"> <img src="img/map/map_graph_3.png" alt="map_image" /> </div>
                                  
                            </div>
                        </div>
                    </div>
                    
                    
                     <div class="row-fluid filter-block">
                        <div class="map_graph_container">
                             <div class="map_graph">
                              	  <img src="img/map/map_graph.png" alt="map_image" /> 
                            </div>
                        </div>
                    </div>
                    
                    

                    <div class ="floatright">
                    <a href="request_form.php" class="btn btn-primary"> Add New </a>
                </div>
                <br/><br/>
               
                    <div class="row-fluid">
                        <table class="table table-hover map-table">
                            <thead>
                                <tr>
                                    <th class="span3 name">
                                        Number  <div class="sort" >
                                        <span class="asc" onclick="window.location='?pageno=<?php echo $currentPageReviews;?>&orderby=asc&field=service_request_number'"></span> 
                                        <span class="desc" onclick="window.location='?pageno=<?php echo $currentPageReviews;?>&orderby=desc&field=service_request_number'"></span></div>
                                    </th>
                                    <th class="span3 title">
                                        Title <div class="sort" >
                                        <span class="asc" onclick="window.location='?pageno=<?php echo $currentPageReviews;?>&orderby=asc&field=Prob_Desc'"></span> 
                                        <span class="desc" onclick="window.location='?pageno=<?php echo $currentPageReviews;?>&orderby=desc&field=Prob_Desc'"></span>
                                    </div>
                                    </th>
                                    <th class="span3 priority">
                                        Priority <div class="sort" >
                                        <span class="asc" onclick="window.location='?pageno=<?php echo $currentPageReviews;?>&orderby=asc&field=Status'"></span> 
                                        <span class="desc" onclick="window.location='?pageno=<?php echo $currentPageReviews;?>&orderby=desc&field=Status'"></span>
                                    </div>
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                <!-- row -->
                                <?php 
                                 if($request_result > 0):
                                foreach($request_result as $value):
                                ?>
                                
                                <tr >
                                    <td>
                                        <a href="request_form.php?id=<?php echo $value['SR_ID']?>">
                                            [#<?php echo $value['service_request_number'];?>]
                                        </a>
                                    </td>
                                    <td class="description">
                                     <?php echo  substr($value['Prob_Desc'], 0, 25);?>
                                    </td>
                                    <td>
                                        <?php 
                                        $class = "";
                                        if($value['Status'] == 'Active'):
                                            $class = 'label-success';
                                        endif;
                                        if($value['Status'] == 'Inactive'):
                                            $class = 'label-important';
                                        endif;
                                        if($value['Status'] == 'Progress'):
                                            $class = 'label-info';
                                        endif;
                                        if($value['Status'] == 'Dispatched'):
                                            $class = 'label-inverse';
                                        endif;
                                        if($value['Status'] == 'Done'):
                                            $class = 'label-success';
                                        endif;
                                        if($value['Status'] == 'Waiting for Info'):
                                            $class = 'label-info';
                                        endif;
                                        if($value['Status'] == 'Hold'):
                                            $class = 'label-warning';
                                        endif;
                                        
                                        
                                        ?>
                                        <span class="label <?php echo $class;?>">#<?php echo $value['Status'];?></span>
                                        
                                        &nbsp;&nbsp;
                                        <a href="service_location.php?id=<?php echo $value['SR_ID'] ?>"> <span class="label label-info">View ServiceMan</span></a>
                                        
                                    </td>
                                </tr>
                                <?php endforeach;?>
                                <tr><td colspan="4" style="text-align: right" > <?php echo $paginator->displayPages();?></td></tr>
                                 <?php  else:?>
                                <tr><td colspan="4"> No record Found</td></tr>
                                <?php endif;?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <!-- end products table -->

                
            </div>
        </div>
    </div>
    <!-- end main container -->

	
<?php 
require_once("includes/footer.php");
?>